<?php

namespace IDock\Insurance\TAIC\Request;

use IDock\IDock\IDockEnums;
use IDock\Insurance\TAIC\Response\UploadFileResponse;
use IDock\Insurance\TAIC\TAICEnums;
use IDock\Visitor\Premium\PremiumInquiryVisitor;

class UploadFileRequest extends AllTAICRequest
{
    protected $route = 'access/car/thirdUploadAttach.mvc';

    protected $response = UploadFileResponse::class;

    protected $urlTip = '影像资料上传';

    protected $inLog = false;

    /*** @var  PremiumInquiryVisitor $visitor */
    protected $visitor;

    public $inquiryNo;

    public $tradeNo;

    public function generate(): array
    {
        return [
            'proposalNo' => $this->inquiryNo,
            'attachmentList' => $this->getFileList(),
        ];
    }

    private function getFileList(): array
    {
        $data = [];

        foreach ($this->visitor->getFileList() as $item) {
            $extension = pathinfo($item['url'])['extension'] ?? 'jpeg';
            $file = file_get_contents($item['url']);
            $item = [
                'fileSize' => strlen($file) / 1024,
                'fileType' => TAICEnums::$uploadFileType[$item['type']],
                'fileName' => IDockEnums::$uploadFileType[$item['type']] . '.' . $extension,
                'fileByteStr' => base64_encode($file),
            ];
            $data[] = $item;
        }
        return $data;
    }
}
